package com.springboot.middleware.middleware_redisson.server.controller;

import com.springboot.middleware.middleware_redisson.api.enums.StatusCode;
import com.springboot.middleware.middleware_redisson.api.response.BaseResponse;
import com.springboot.middleware.middleware_redisson.server.dto.DeadDto;
import com.springboot.middleware.middleware_redisson.server.rabbitmq.MqDelayQueuePublisher;
import com.springboot.middleware.middleware_redisson.server.redisson.QueuePublisher;
import com.springboot.middleware.middleware_redisson.server.redisson.RedissonDelayQueuePublisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class QueueController {
    private static final Logger log = LoggerFactory.getLogger(QueueController.class);
    private static final String prefix = "queue";
    @Autowired
    QueuePublisher queuePublisher;
    @Autowired
    MqDelayQueuePublisher mqDelayQueuePublisher;

    @RequestMapping(value = prefix + "/basic/msg/send")
    public BaseResponse sendBasicMsg(@RequestParam String msg) {
        BaseResponse baseResponse = new BaseResponse(StatusCode.Success);
        try {
            queuePublisher.sendBasicMsg(msg);
        } catch (Exception e) {
            baseResponse = new BaseResponse(StatusCode.Fail.getCode(), e.getMessage());
        }
        return baseResponse;
    }

    @RequestMapping(value = prefix + "/basic/msg/delay/send", method = RequestMethod.GET)
    public BaseResponse sendMQDelayMsg() {
        BaseResponse baseResponse = new BaseResponse(StatusCode.Success);
        try {
            DeadDto msgA = new DeadDto(1, "A");
            final Long ttlA = 10000L;
            DeadDto msgB = new DeadDto(2, "B");
            final Long ttlB = 3000L;
            DeadDto msgC = new DeadDto(3, "C");
            final Long ttlC = 5000L;
            mqDelayQueuePublisher.sendDelayMsg(msgA, ttlA);
            mqDelayQueuePublisher.sendDelayMsg(msgB, ttlB);
            mqDelayQueuePublisher.sendDelayMsg(msgC, ttlC);
        } catch (Exception e) {
            baseResponse = new BaseResponse(StatusCode.Fail.getCode(), e.getMessage());
        }
        return baseResponse;
    }

    @Autowired
    RedissonDelayQueuePublisher redissonDelayQueuePublisher;

    @RequestMapping(value = prefix+"/redisson/msg/delay/send", method = RequestMethod.GET)
    public BaseResponse sendRedissonDelayMsg() {
        BaseResponse baseResponse = new BaseResponse(StatusCode.Success);
        try {
            DeadDto msgA = new DeadDto(1, "A");
            final Long ttlA = 10000L;
            DeadDto msgB = new DeadDto(2, "B");
            final Long ttlB = 10000L;
            DeadDto msgC = new DeadDto(3, "C");
            final Long ttlC = 9001L;

            redissonDelayQueuePublisher.sendDelayMsg(msgA, ttlA);
            redissonDelayQueuePublisher.sendDelayMsg(msgB, ttlB);
            redissonDelayQueuePublisher.sendDelayMsg(msgC, ttlC);
        } catch (Exception e) {
            baseResponse = new BaseResponse(StatusCode.Fail.getCode(), e.getMessage());
        }
        return baseResponse;
    }
}
